// DlgInsertTable.cpp : implementation file
//

#include "stdafx.h"
#include "ansieditor.h"
#include "DlgInsertTable.h"
#include "DlgTableStyle.h"
#include "TableResizer.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgInsertTable dialog


CDlgInsertTable::CDlgInsertTable(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgInsertTable::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgInsertTable)
	m_nBorderStyle = -1;
	m_nColCount = 0;
	m_nColWidth = 0;
	m_nRowCount = 0;
	m_nRowHeight = 0;
	//}}AFX_DATA_INIT
}


void CDlgInsertTable::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgInsertTable)
	DDX_Control(pDX, IDC_SPN_ROW_HEIGHT, m_spnRowHeight);
	DDX_Control(pDX, IDC_SPN_ROW_COUNT, m_spnRowCount);
	DDX_Control(pDX, IDC_SPN_COL_WIDTH, m_spnColWidth);
	DDX_Control(pDX, IDC_SPN_COL_COUNT, m_spnColCount);
	DDX_Radio(pDX, IDC_OPT_BORDER_STYLE, m_nBorderStyle);
	DDX_Text(pDX, IDC_TXT_COL_COUNT, m_nColCount);
	DDV_MinMaxUInt(pDX, m_nColCount, 1, 64);
	DDX_Text(pDX, IDC_TXT_COL_WIDTH, m_nColWidth);
	DDV_MinMaxUInt(pDX, m_nColWidth, 0, 64);
	DDX_Text(pDX, IDC_TXT_ROW_COUNT, m_nRowCount);
	DDV_MinMaxUInt(pDX, m_nRowCount, 1, 512);
	DDX_Text(pDX, IDC_TXT_ROW_HEIGHT, m_nRowHeight);
	DDV_MinMaxUInt(pDX, m_nRowHeight, 0, 64);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgInsertTable, CDialog)
	//{{AFX_MSG_MAP(CDlgInsertTable)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgInsertTable message handlers


BOOL CDlgInsertTable::OnInitDialog() 
{
	m_nBorderStyle = gpApp->GetProfileInt("Dialog", "InsertTable.BorderStyle", 0);
	m_nColCount = gpApp->GetProfileInt("Dialog", "InsertTable.ColCount", 2);
	m_nColWidth = gpApp->GetProfileInt("Dialog", "InsertTable.ColWidth", 1);
	m_nRowCount = gpApp->GetProfileInt("Dialog", "InsertTable.RowCount", 2);
	m_nRowHeight = gpApp->GetProfileInt("Dialog", "InsertTable.RowHeight", 1);

	CDialog::OnInitDialog();
	
	m_spnColCount.SetRange32(1, 64);
	m_spnColWidth.SetRange32(0, 64);
	m_spnRowCount.SetRange32(1, 512);
	m_spnRowHeight.SetRange32(0, 64);

	
	return TRUE;
}

void CDlgInsertTable::OnOK() 
{
	UpdateData();

	gpApp->WriteProfileInt("Dialog", "InsertTable.BorderStyle", m_nBorderStyle);
	gpApp->WriteProfileInt("Dialog", "InsertTable.ColCount", m_nColCount);
	gpApp->WriteProfileInt("Dialog", "InsertTable.ColWidth", m_nColWidth);
	gpApp->WriteProfileInt("Dialog", "InsertTable.RowCount", m_nRowCount);
	gpApp->WriteProfileInt("Dialog", "InsertTable.RowHeight", m_nRowHeight);

	m_sTableText.Empty();
	BYTE pinDirs;
	int borderStep = m_nBorderStyle > 0? 2: 1;
	for(UINT r = 0; r < m_nRowCount + 1; r++)
	{
		pinDirs = 0;
		if(r > 0)
		{
			pinDirs |= PIN_DIR_TOP;
		}
		if(r < m_nRowCount)
		{
			pinDirs |= PIN_DIR_BOTTOM;
		}
		for(UINT c = 0; c < m_nColCount + 1; c++)
		{
			BYTE pinDirs1 = pinDirs;
			if(c > 0)
			{
				pinDirs1 |= PIN_DIR_LEFT;
			}
			if(c < m_nColCount)
			{
				pinDirs1 |= PIN_DIR_RIGHT;
			}
			m_sTableText += CDlgTableStyle::GetTableChar(m_nBorderStyle, pinDirs1);
			if(c < m_nColCount)
			{
				int nCount = int(1.0 * m_nColWidth / borderStep + 0.5);
				for(int n = 0; n < nCount; n++)
				{
					m_sTableText += CDlgTableStyle::GetTableChar(m_nBorderStyle, PIN_DIR_LEFT|PIN_DIR_RIGHT);
				}
			}
		}
		if(r < m_nRowCount)
		{
			for(UINT r1 = 0; r1 < m_nRowHeight; r1++)
			{
				m_sTableText += "\r\n";
				for(UINT c = 0; c < m_nColCount + 1; c++)
				{
					m_sTableText += CDlgTableStyle::GetTableChar(m_nBorderStyle, PIN_DIR_TOP|PIN_DIR_BOTTOM);
					if(c < m_nColCount)
					{
						int nCount = int(1.0 * m_nColWidth / borderStep + 0.5);
						for(int n = 0; n < nCount; n++)
						{
							m_sTableText += CString(' ', borderStep);
						}
					}
				}
			}
			m_sTableText += "\r\n";
		}
	}
	m_tableSize.cx = borderStep *( (m_nColCount + 2) + int(1.0 * m_nColWidth / borderStep + 0.5) * m_nColCount );
	m_tableSize.cy = m_nRowCount + 2 + m_nRowHeight * m_nRowCount;

	CDialog::OnOK();
}